package com.hq.ims.userpc.controller;

import com.hq.authority.utils.AppUtils;
import com.hq.ims.data.entity.BusiChange;
import com.hq.ims.data.service.BaseCompanyService;
import com.hq.ims.data.service.BaseUserService;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.utils.WebExpandUtils;
import com.hq.ims.data.vo.CompanyChangeHistoryMapstruct;
import com.hq.ims.data.vo.CompanyMapstruct;
import com.hq.ims.data.vo.PermissionMenuMapstruct;
import com.hq.ims.data.vo.UserMapstruct;
import com.hq.ims.userpc.busservice.UserBusiInfoService;
import com.hq.ims.userpc.busservice.UserPcMyInfoBusService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.mybatis.base.page.ReturnPage;
import com.hq.utils.vo.ReturnVo;
import com.hq.web.annotations.GetJsonValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "我的信息")
@RestController
@RequestMapping(value = LocalConstant.SYS_TYPE_USER_PC_URL_PREFIX + "/home/myInfo")
@Slf4j
public class UserPcMyInfoController {

    @Resource
    private UserPcMyInfoBusService userPcMyInfoBusService;
    @Resource
    private UserBusiInfoService userBusiInfoService;
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private BaseCompanyService baseCompanyService;

    @ApiOperation(value = "获取用户拥有的权限菜单", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/getHaveAuthMenu", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<List<PermissionMenuMapstruct.PermissionMenuTreeVo>> getHaveAuthMenu(
    ) {
        List<PermissionMenuMapstruct.PermissionMenuTreeVo> sysMenuVoList = userPcMyInfoBusService.getHaveAuthMenu();
        return ReturnVo.buildSuccess(sysMenuVoList);
    }

    @ApiOperation(value = "修改我的密码", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/updateMyPwd", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> updateMyPwd(
            @RequestBody UserMapstruct.ForgotPasswordVo forgotPasswordVo,
            @GetJsonValue(value = "验证码不能为空") String smsCode
    ) {
        baseUserService.updateUserPwdValidSmsCode(smsCode, forgotPasswordVo);
        return ReturnVo.buildSuccess(true);
    }

    @ApiOperation(value = "获取我的个人信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/getMyInfo", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<UserMapstruct.UserAndCompanySimpleVo> getMyInfo(
    ) {
        UserMapstruct.UserAndCompanySimpleVo myInfo = userPcMyInfoBusService.getMyInfo();
        return ReturnVo.buildSuccess(myInfo);
    }

    @ApiOperation(value = "我的企业", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/myCompanyInfo", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<CompanyMapstruct.CompanyVo> myCompanyInfo(@RequestBody CompanyMapstruct.CompanyVo companyVo) {
        CompanyMapstruct.CompanyVo vo = userPcMyInfoBusService.myCompanyInfo(companyVo);
        return ReturnVo.buildSuccess(vo);
    }

    @ApiOperation(value = "用户加入企业", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/joinCompany", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> joinCompany(@GetJsonValue(zh = "公司ID") String companyId) {
        userPcMyInfoBusService.joinCompany(companyId);
        return ReturnVo.buildSuccess(true);
    }

    @ApiOperation(value = "用户登陆后注册企业", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/userLoginAfterCreateCompany", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> userLoginAfterCreateCompany(
            @RequestBody CompanyMapstruct.UserRegisterNewCompanySaveVo saveVo
    ) {
        userPcMyInfoBusService.userLoginAfterCreateCompany(saveVo);
        return ReturnVo.buildSuccess(true);
    }

    @ApiOperation(value = "修改我的企业", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/updateCompanyInfo", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> updateCompanyInfo(@RequestBody CompanyMapstruct.CompanyVo companyVo) {
        boolean b = userPcMyInfoBusService.updateCompanyInfo(companyVo);
        return ReturnVo.buildSuccess(b);
    }

    @ApiOperation(value = "获取登陆后状态", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/getLoginAfterStatus", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<UserMapstruct.LoginSuccessAfterReturnStatus> getLoginAfterStatus(
    ) {
        UserMapstruct.LoginSuccessAfterReturnStatus loginAfterStatus = userPcMyInfoBusService.getLoginAfterStatus(
                AppUtils.getUserId(),
                WebExpandUtils.getHeadCompanyId()
        );
        return ReturnVo.buildSuccess(loginAfterStatus);
    }

    @ApiOperation(value = "查询企业审核历史记录", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getCompanyChangeHistory", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<ReturnPage<BusiChange>> getCompanyCheckHistoryData(@RequestBody RequestPage page, @RequestBody CompanyChangeHistoryMapstruct.CompanyChangeHistoryVo companyChangeHistory) {
        ReturnPage<BusiChange> changes = baseCompanyService.getCompanyChangeHistory(page, companyChangeHistory);
        return ReturnVo.buildSuccess(changes);
    }


    @ApiOperation(value = "撤销企业审核", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/cancelCompanyCheck", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo cancelCompanyCheck(@RequestBody CompanyMapstruct.CompanyVo companyVo) {
        boolean b = baseCompanyService.cancelCompanyCheck(companyVo);
        return ReturnVo.buildSuccess(b);
    }

}
